package com.huihui.activity.login;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.huihui.R;
import com.huihui.base.BaseActivity;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * 忘记密码
 */
public class ReSetPswActivity extends BaseActivity {

    private EditText input_phone;
    private EditText input_psw;
    private TextView get_sms;
    private EditText input_new_psw;
    private EditText input_comfirm_psw;
    private TextView comfirm;


    private boolean mIsPhoneValid;
    private int mCount = 60;
    private int type;


    private Runnable mCountDownRunnable = new Runnable() {
        @Override
        public void run() {
            if (mCount > 0) {
                get_sms.setText(getString(R.string.control_get_code_counter, mCount--));
                UIHandler.postDelayed(mCountDownRunnable, 1000);
            } else {
                mCount = 60;
                get_sms.setText(getString(R.string.get_sms));
                if (mIsPhoneValid) {
                    get_sms.setEnabled(true);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_re_set_psw);
//        getSupportActionBar().setTitle(R.string.forget_psw);
        titleView.setText(R.string.forget_psw);
        initView();
        initListener();
    }

    private void initListener() {
        comfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断
                reset();
            }
        });

        get_sms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSms();
            }

        });
    }

    private void reset() {
        String phone = input_phone.getText().toString();
        String psw = input_psw.getText().toString();
        String new_psw = input_new_psw.getText().toString();
        String new_psw_confirm = input_comfirm_psw.getText().toString();
        if (!StringUtils.isPhone(phone)) {
            Toast.makeText(getContext(),R.string.error_phone_not_match,Toast.LENGTH_SHORT).show();
            return;
        }
        if(!new_psw.equals(new_psw_confirm)){
            Toast.makeText(getContext(),R.string.error_phone_butong,Toast.LENGTH_SHORT).show();
            return;
        }
        if(new_psw.length() < 6){
            Toast.makeText(getContext(),"密码不能少于6位数",Toast.LENGTH_SHORT).show();
            return;
        }
        OkHttpRequest.getInstance().forgetPsw(HttpContants.forgetPsw,  phone, psw, new_psw, new_psw_confirm, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject json = new JSONObject(response);
                    if(json != null && json.has(HttpContants.STATE) && json.getInt(HttpContants.STATE) == 10000) {
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void getSms() {

        //获取验证码
        String inputPhone = input_phone.getText().toString();
        if (!StringUtils.isPhone(inputPhone)) {
            Toast.makeText(getContext(),R.string.error_phone_not_match,Toast.LENGTH_SHORT).show();
            return;
        }
        get_sms.setEnabled(false);
        get_sms.setText(getString(R.string.control_get_code_counter, mCount--));
        //获取验证码请求
        OkHttpRequest.getInstance().sendSmsCode(HttpContants.sendSmsCode, 0, input_phone.getText().toString(), 2, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                get_sms.setEnabled(true);
                mCount = 60;
                get_sms.setText(getString(R.string.get_sms));
                UIHandler.removeCallbacks(mCountDownRunnable);
            }

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject json = new JSONObject(response);
                    if(json.has(HttpContants.MSG)){
                        Toast.makeText(getContext(), json.getString(HttpContants.MSG),Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        UIHandler.postDelayed(mCountDownRunnable, 1000);
    }

    private void initView() {
        input_phone = (EditText) findViewById(R.id.input_phone);
        input_psw = (EditText) findViewById(R.id.input_psw);
        get_sms = (TextView) findViewById(R.id.get_sms);
        input_new_psw = (EditText) findViewById(R.id.input_new_psw);
        input_comfirm_psw = (EditText) findViewById(R.id.input_comfirm_psw);
        comfirm = (TextView) findViewById(R.id.comfirm);
        type = getIntent().getIntExtra("type",0);
    }
}
